//
//  MessageViewController.m
//  BrushFace
//
//  Created by timer_q on 15/12/4.
//  Copyright © 2015年 Zzz. All rights reserved.
//

#import "MessageViewController.h"
#import "MessageActionCell.h"
#import "MessageDefaultCell.h"
#import "FindViewModel.h"
#import "NavHelper.h"
@interface MessageViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *_messageArray;
    int _currentPage;
}

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) FindViewModel *reqmodel;
@end

@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self messageDataInit];
    [self setRefresh];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    [_reqmodel fetchFindListWithRPP:20 andCurPage:0 andCB:^(id output) {
        [_messageArray removeAllObjects];
        [_messageArray addObjectsFromArray:output[@"data"]];
        [_tableView reloadData];
    }];
    
}
- (void)messageDataInit{
    [self.navBar setTitle:@"消息中心"];
    [self.navBar setLeftBtn:NAV_BAR_LEFT_BTN_TYPE_BLUE];
    _messageArray = [NSMutableArray array];
    [self.tableView reloadData];
    _reqmodel = [[FindViewModel alloc] init];
    
}
#pragma mark - tableViewDelegate And DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _messageArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return 110;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellActionID = @"MessageActionCell";
    
    NSDictionary *dict = [_messageArray objectAtIndex:indexPath.section];
    
    MessageActionCell *cell = [tableView dequeueReusableCellWithIdentifier:cellActionID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:cellActionID owner:self options:nil] lastObject];
    }
    switch ([[NSString stringWithFormat:@"%@",dict[@"type"]] integerValue]) {
            case 1:
        {
            cell.actionLabel.text = @"立即出借";
        }
            break;
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 8:
            case 11:
            case 12:
        {
            cell.actionLabel.text = @"立即查看";
        }
            break;
            case 7:
        {
            cell.actionLabel.text = @"立即出借";
        }
            break;
            case 9:
            case 10:
        {
            cell.actionLabel.text = @"立即还款";
        }
            break;
        default:
            break;
    }
    cell.detailLabel.text = [NSString stringWithFormat:@"%@",dict[@"content"]];
    cell.titleLabel.text = [NSString stringWithFormat:@"%@",dict[@"title"]];
    if ([[NSString stringWithFormat:@"%@",dict[@"readed"]] integerValue]==0) {
        cell.LittleRedPoint.hidden = NO;
    }else{
        cell.LittleRedPoint.hidden = YES;
    }
    cell.dateLabel.text = [self timerStrWithTime:dict[@"time"]];
    
    return cell;
}
#pragma mark------qiweipeng
//添加了下载刷新

- (void)setRefresh{
    @weakify (self);
    [_tableView addPullToRefreshWithActionHandler:^{
        @strongify (self);
        self->_currentPage = 0;
        [self->_reqmodel fetchFindListWithRPP:10 andCurPage:0 andCB:^(id output) {
            [self->_messageArray removeAllObjects];
            [self->_messageArray addObjectsFromArray:output[@"data"]];
            [self->_tableView reloadData];
            [self->_tableView endAllRefreshingWithEnd:NO];
            NSLog(@"%d",self->_currentPage);
        }];
    }];
    [_tableView addInfiniteScrollingWithActionHandler:^{
        @strongify (self);
        [self->_reqmodel fetchFindListWithRPP:10 andCurPage:++self->_currentPage andCB:^(id output) {
            NSMutableArray *resultArray = output[@"data"];
            [self->_messageArray addObjectsFromArray:resultArray];
            [self->_tableView reloadData];
            if (resultArray.count<10) {
                [self->_tableView endAllRefreshingWithEnd:YES];
            }else{
                [self->_tableView endAllRefreshingWithEnd:NO];
            }
            NSLog(@"%d",self->_currentPage);
        }];
    }];
    
}
#pragma mark----------------------

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dict = [_messageArray objectAtIndex:indexPath.section];
    [_reqmodel readMessageWithId:[NSString stringWithFormat:@"%@",dict[@"id"]] andCB:^(id output) {
        
    }];
    switch ([[NSString stringWithFormat:@"%@",dict[@"type"]] integerValue]) {
        case 1:
        {
            [NavHelper gotoAssetsRecordVC:1 andBaseType:TYPE_FINANCE andHopeId:dict[@"relatedid"]];
        }
            break;
        case 2:
        {
            [NavHelper gotoAssetsRecordVC:1 andBaseType:TYPE_BORROW_IN andHopeId:dict[@"relatedid"]];
        }
            break;
        case 3:
        {
            [NavHelper gotoAssetsRecordVC:1 andBaseType:TYPE_BORROW_OUT andHopeId:dict[@"relatedid"]];
        }
            break;
        case 4:
        {
            [NavHelper gotoAssetsRecordVC:7 andBaseType:TYPE_BORROW_OUT andHopeId:dict[@"relatedid"]];
        }
            break;
        case 5:
        {
            [NavHelper gotoAssetsRecordVC:7 andBaseType:TYPE_BORROW_IN andHopeId:dict[@"relatedid"]];
        }
            break;
        case 6:
        {
            [NavHelper gotoAssetsRecordVC:2 andBaseType:TYPE_BORROW_OUT andHopeId:dict[@"relatedid"]];
        }
            break;
        case 8:
        {
            [NavHelper gotoMeBalance];
        }
            break;
        case 11:
        {
            [NavHelper gotoAssetsRecordVC:4 andBaseType:TYPE_BORROW_OUT andHopeId:dict[@"relatedid"]];
        }
            break;
        case 12:
        {
            [NavHelper gotoAssetsRecordVC:5 andBaseType:TYPE_BORROW_OUT andHopeId:dict[@"relatedid"]];
        }
            break;
        case 7:
        {
            [NavHelper gotoAssetsRecordVC:2 andBaseType:TYPE_BORROW_IN andHopeId:dict[@"relatedid"]];
        }
            break;
        case 9:
        {
            [NavHelper gotoAssetsRecordVC:4 andBaseType:TYPE_BORROW_IN andHopeId:dict[@"relatedid"]];
        }
            break;
        case 10:
        {
            [NavHelper gotoAssetsRecordVC:5 andBaseType:TYPE_BORROW_IN andHopeId:dict[@"relatedid"]];
        }
            break;
        default:
            break;
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (NSString *)timerStrWithTime:(NSString *)time{
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:[time doubleValue]/1000+28800];
    //NSLog(@"date:%@",[detaildate description]);
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    return currentDateStr;
}

@end
